<?php
/**
 * 定义 Registry 类
 * 
 * @copyright Copyright (c) 2008 Classic Reappear Technology LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version $Id: registry.php 2009-2-27 下午02:18:14 蓝色身份 $
 */

/**
 * 注册表读写类
 *
 * @author 蓝色身份 <lance.w@live.cn>
 * @copyright Copyright (c) 2008 Classic Reappear Technology LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package Registry
 */
class Registry extends ArrayObject
{
    /**
     * 存储单件模式实例
     *
     * @var Registry
     */
    private static $_instance = null;

    /**
     * 单件模式调用方法
     *
     * @return Registry
     */
    public static function getInstance()
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 设置指定项的值
     *
     * @param string $name
     * @param mixed $value
     */
    public static function set($name, $value)
    {
        self::getInstance()->offsetSet($name, $value);
    }

    /**
     * 获取指定项的值
     *
     * @param string $name
     * @return mixed
     */
    public static function get($name)
    {
        $instance = self::getInstance();
        if (!$instance->offsetExists($name)) {
            throw new Exception("No entry is registered for key \"$name\"");
        }
        return $instance->offsetGet($name);
    }

    /**
     * 判断指定项是否存在
     *
     * @param string $name
     * @return boolean
     */
    public static function isRegistered($name)
    {
        return self::getInstance()->offsetExists($name);
    }

    /**
     * 卸载指定项
     *
     * @param string $name
     */
    public static function remove($name)
    {
        self::getInstance()->offsetUnset($name);
    }
}